import store from '@/store'
var Enumerable = require('linq');
/**
 * 根据路由的meta.permission 判断是否显示
 * @param {String} permission 路由的meta.permission 
 * @param {Array} menus 登录返回menus 菜单权限
 * @returns {Boolean} 是否有权限
 */
export function checkPermission(permission, menus) {
    if (!menus) {
        menus = store.getters && store.getters.menus
    }
    var modules = [];
    var resources = [];
    var operates = [];
    if (permission.module_code) {
        modules = permission.module_code.split(',');
    }
    if (permission.resource_code) {
        resources = permission.resource_code.split(',');
    }
    if (permission.operate_code) {
        operates = permission.operate_code.split(',');
    }
    if (modules.length > 0) {
        var check_modules_list = Enumerable.from(menus).selectMany((item) => item.modules).where(function(item) { return Enumerable.from(modules).contains(item.module_code); }).toArray();
        if (check_modules_list.length <= 0) {
            return false;
        }
        if (resources.length > 0) {
            var check_resources_list = Enumerable.from(check_modules_list).selectMany((item) => item.resorces).where(item => Enumerable.from(resources).contains(item.res_code)).toArray();
            console.log(check_resources_list);
            if (check_resources_list.length <= 0) {
                return false;
            }

            if (operates.length > 0) {
                var check_operates_list = Enumerable.from(check_resources_list).selectMany((item) => item.operates).where(item => Enumerable.from(operates).contains(item.operate_code)).toArray();
                console.log(check_operates_list);
                if (check_operates_list.length <= 0) {
                    return false;
                }
            }
        }
    }
    return true //roles.some(role =>roles.includes(role))
}

/**
 * 根据路由的meta.accounttype 判断是否显示
 * @param {Array}} meta_account_type 路由的meta.accounttype 
 * @param {String} account_type 登录返回user_data.account_type 账号所属的身份
 * @returns {Boolean} 是否有权限
 */
export function CheckAccountType(meta_account_type, account_type) {
    if (!account_type) {
        account_type = store.getters && store.getters.account_type
    }
    if (account_type == 'admin') {
        return true;
    }
    if (meta_account_type.includes(meta_account_type)) {
        return true;
    }
    return false;
}